package com.emergency.db.service.impl.nuclear;

import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.db.dao.nuclear.NuclearInsideDao;
import com.emergency.db.dao.nuclear.ShellDao;
import com.emergency.db.dao.nuclear.VesselDao;
import com.emergency.db.domain.nuclear.NuclearInside;
import com.emergency.db.domain.nuclear.Shell;
import com.emergency.db.domain.nuclear.Vessel;
import com.emergency.db.service.nuclear.NuclearInsideService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class NuclearInsideServiceImpl implements NuclearInsideService {
    private final static Logger logger = LoggerFactory.getLogger(NuclearInsideServiceImpl.class);

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private NuclearInsideDao nuclearInsideDao;

    @Autowired
    private VesselDao vesselDao;

    @Autowired
    private ShellDao shellDao;

    //保存
    public Long save(NuclearInside nuclearInside) {
        if (CommonUtils.isNullOrEmpty(nuclearInside.getId())) {
            nuclearInside.setId(idWorker.nextId());
            nuclearInside.setCreateTime(new Date());
        }
        //保存涉核目标基本信息
        nuclearInsideDao.save(nuclearInside);
        //保存涉核目标压力容器信息
        vesselSave(nuclearInside);
        //保存涉核目标安全壳信息
        shellSave(nuclearInside);
        return nuclearInside.getId();
    }

    private void vesselSave(NuclearInside nuclearInside) {
        Vessel vessel = nuclearInside.getVessel();
        if (CommonUtils.isNullOrEmpty(vessel.getId())) {
            vessel.setId(idWorker.nextId());
            vessel.setInside(nuclearInside);
        }
        vesselDao.save(vessel);
    }

    private void shellSave(NuclearInside nuclearInside) {
        Shell shell = nuclearInside.getShell();
        if (CommonUtils.isNullOrEmpty(shell.getId())) {
            shell.setId(idWorker.nextId());
            shell.setInside(nuclearInside);
        }
        shellDao.save(shell);
    }


    //查询全部
    @Override
    public List<NuclearInside> findAll() {
        List<NuclearInside> nuclearInsides = nuclearInsideDao.findAll();
        nuclearInsides.forEach(nuclearInside -> {
            nuclearInside.setVessel(vesselDao.findByNuclear(nuclearInside.getId()));
            nuclearInside.setShell(shellDao.findByNuclear(nuclearInside.getId()));
        });
        return nuclearInsides;
    }

    //查询单项
    public NuclearInside find(Long id) {
        NuclearInside nuclearInside = nuclearInsideDao.findById(id).orElse(null);
        if (CommonUtils.isNullOrEmpty(nuclearInside)) {
            logger.info("此员工信息为空！");
            return nuclearInside;
        }
        nuclearInside.setShell(shellDao.findByNuclear(id));
        nuclearInside.setVessel(vesselDao.findByNuclear(id));
        return nuclearInside;
    }

    @Override
    public void delete(Long id) {
        vesselDao.deleteByNuclear(id);
        shellDao.deleteByNuclear(id);
        nuclearInsideDao.deleteById(id);
    }

    //分页查询
    @Override
    public Page<NuclearInside> findList(Map searchMap, int page, int size) {
        Specification<NuclearInside> specification = createSpecification(searchMap);
        Sort sort = Sort.by(Sort.Direction.ASC, "createTime");
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        Page<NuclearInside> nuclearInsides = nuclearInsideDao.findAll(specification, pageRequest);
        nuclearInsides.forEach(nuclearInside -> {
            nuclearInside.setVessel(vesselDao.findByNuclear(nuclearInside.getId()));
            nuclearInside.setShell(shellDao.findByNuclear(nuclearInside.getId()));
        });
        return nuclearInsides;
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<NuclearInside> createSpecification(Map searchMap) {
        return new Specification<NuclearInside>() {

            @Override
            public Predicate toPredicate(Root<NuclearInside> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<Predicate>();

                // 名称
                if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                    predicateList.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + (String) searchMap.get("name") + "%"));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}
